<?php
/**
 * Created by PhpStorm.
 * User: macmini
 * Date: 2018/2/28
 * Time: 下午4:17
 */

namespace app\index\controller;

use app\common\controller\Home;

class Post extends Home {

    public function index() {
        return $this->fetch();
    }

    public function detail() {
        $post_id = request()->get('id');

        $result = model('Posts')->get($post_id);
        print_r($result->toArray());
        echo "<hr>\n";

        $share_data = [
            'share_id' => 1
        ];
        $share_data = json_encode($share_data);
        $base = base64_encrypt($share_data, 'wly');
        echo  $base. "<hr>\n";
        echo base64_decrypt($base, 'wly') . "<hr>\n";

        echo uniqid() . "<hr>\n";

        echo $post_id . "<hr>\n";
        echo config('token_key') . "<hr>\n";

        $ip = request()->ip();
        echo $ip . "<br>\n";
        echo ip2long($ip) . "<br>\n";
        $ua = $_SERVER['HTTP_USER_AGENT'];
        echo $ua . "<br>\n";
        echo sha1($ua) . "<br>\n";








        $token = request()->get('token');

        $share_id = $this->get_share_id($token);
        //
        $analytics_data = [
            'post_id' => $post_id,
            'share_id' => $share_id,
            'url' => get_url(),
            'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
            'ip_id' => $this->get_ip_id(),
            'ua_id' => $this->get_ua_id(),
            'add_time' => time(),
        ];
        model('Analytics')->insert($analytics_data,false,true);


        $has_share_id = model('Shares')->where("share_id", $share_id)->value("share_id");
        if ($has_share_id) {
            //
            model("Shares")->where("share_id", $share_id)->setInc("share_clicks");
        }



        return $this->fetch();
    }

    public function article($id)
    {
        $check_res = $this->check_browser();
        if (!$check_res) {
            return $this->redirect("http://tech.sina.com.cn/mobile/n/n/2018-03-19/doc-ifyskeuc0587240.shtml");
        }
        $info = base64_decrypt($id, config('token_key'));
        if (!$info) {
            abort(404, '错误请求');
        }
        $info_arr = json_decode($info, true);
        if (!isset($info_arr['user_id']) || !isset($info_arr['post_id'])) {
            abort(404, '错误请求');
        }
        $result = model('Posts')->get($info_arr['post_id']);
        $post_info = $result->toArray();
        $post_content = model('PostContents')->where('post_id', $info_arr['post_id'])->find();
        $ip_id = $this->get_ip_id();
        $ua_id = $this->get_ua_id();
        $this->get_analytics($info_arr, $ip_id, $ua_id);
        $view_id = $this->create_view_log($info_arr, $ip_id, $ua_id);
        $view_token = $this->get_view_token($view_id);
        $ads = $this->get_ads();
        $charging = new Charging();
        $config = $charging->get_config();
        $this->assign([
            'post_info' => $post_info,
            'post_content' => $post_content['post_content'],
            'view_token' => $view_token,
            'ads' => $ads,
            'billing_time' => $config['billing_time'] * 1000,
        ]);
        return $this->fetch();
    }


    private function create_view_log($info_arr, $ip_id, $ua_id)
    {
        $has_share_id = model('Shares')->where("share_id", $info_arr['share_id'])->value("share_id");
        if ($has_share_id) {
            model("Shares")->where("share_id", $info_arr['share_id'])->setInc("share_clicks");
            model("Users")->where("user_id", $info_arr['user_id'])->setInc("total_views");
            $model = model('ViewLogs');
            $search_where = [
                'post_id' => $info_arr['post_id'],
                'user_id' => $info_arr['user_id'],
                'share_id'  => $info_arr['share_id'],
                'ip_id' => $ip_id,
                'ua_id' => $ua_id,
            ];
            $view_data = [
                'post_id' => $info_arr['post_id'],
                'user_id' => $info_arr['user_id'],
                'share_id'  => $info_arr['share_id'],
                'ip_id' => $ip_id,
                'ua_id' => $ua_id,
                'add_time' => time(),
                'add_time_f' => date('Ymd'),
            ];
            $info = $model->where($search_where)->find();
            if ($info) {
                $view_data ['status'] = 3;
                $model->save($view_data);
                return 0;
            } else {
                $view_data ['status'] = 0;
                $model->save($view_data);
                return $model->view_id;
            }
            
        }
    }

    private function get_ads()
    {
        $model = model('Ads');
        $where = [
            'ad_status' => 1,
        ];
        $ads = $model->where($where)->select();
        return $ads;
    }

    private function get_analytics($info_arr, $ip_id, $ua_id)
    {
        $analytics_data = [
            'post_id' => $info_arr['post_id'],
            'share_id' => $info_arr['share_id'],
            'url' => get_url(),
            'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
            'ip_id' => $ip_id,
            'ua_id' => $ua_id,
            'add_time' => time(),
        ];
        model('Analytics')->insert($analytics_data,false,true);
    }

    private function get_view_token($view_id)
    {

        $data = [
            'view_id' => $view_id
        ];
        return base64_encrypt(json_encode($data), config('view_key'));
    }

    /**
     * 检测请求是否合法
     * @return bool
     */
    private function check_browser()
    {
        return true;
    }

}
